<?php
/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/2/27 0027
 * Time: 下午 17:51
 */
Class LoginAction extends Action{
    
    //登入首页视图
    public function index(){
        $this->display();
    }
    
    //登入验证
    public function login(){
//        p(md5('admin'));
        if(!IS_POST){//禁止非表单提交访问
            halt('页面不存在');
        }
        if(I('code','','md5')==$_SESSION['verify']){
            $db=M('user');
            $user=$db->where(array('username'=>I('username')))->find();
            if ($user&&I('password','','md5')==$user['password']){
                $date=array(
                    'userId'=>$user['id'],
                    'logintime'=>time(),
                    'loginip'=>get_client_ip(1)
                );
                $db->save($date);
                session('logintime',date('Y-m-d H:i:s'),$user['logintime']);
                session('loginip',$user['loginip']);
                session(C('USER_AUTH_KEY'),$user['id']);
                session('username',$user['username']);

                //超级管理员识别
                if ($user['username']==C('RBAC_SUPERADMIN')){
                    session(C('ADMIN_AUTH_KEY'),true);
                }

                redirect(__GROUP__);
            }else{
                $this->error('用户不存在或密码错误');
            }
        }else{
            $this->error('验证码错误');
        }
    }

    //验证码图片
    Public function verify(){
        import('ORG.Util.Image');
        Image::buildImageVerify();
    }
}